package org.mixare.placesint.data.convert;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mixare.lib.HtmlUnescape;
import org.mixare.lib.marker.Marker;
import org.mixare.placesint.MixView;
import org.mixare.placesint.POIMarker;
import org.mixare.placesint.data.DataHandler;
import org.mixare.placesint.data.DataSource;

import android.util.Log;

public class LocalDataProcessor extends DataHandler implements DataProcessor {

	public static final int MAX_JSON_OBJECTS = 1000;

	@Override
	public String[] getUrlMatch() {
		String[] str = { "local" };
		// String url_local = "http://ale90system.p.ht/mixData.php";
		return str;
	}

	@Override
	public String[] getDataMatch() {
		String[] str = { "local" };
		return str;
	}

	@Override
	public boolean matchesRequiredType(String type) {
		if (type.equals(DataSource.TYPE.LOCAL.name())) {
			return true;
		}
		return false;
	}

	@Override
	public List<Marker> load(String rawData, int taskId, int colour)
			throws JSONException {
		List<Marker> markers = new ArrayList<Marker>();
		JSONObject root = convertToJSON(rawData);
		JSONArray dataArray = root.getJSONArray("results");
		int top = Math.min(MAX_JSON_OBJECTS, dataArray.length());

		for (int i = 0; i < top; i++) {
			JSONObject jo = dataArray.getJSONObject(i);

			Marker ma = null;
			if (jo.has("title") && jo.has("lat") && jo.has("lng")
					&& jo.has("elevation")) {

				String id = "";
				if (jo.has("id")) {
					id = jo.getString("id");
				}

				Log.v(MixView.TAG, "processing Local JSON object");
				String link = "http://ale90system.p.ht/mixData.php";

				if (jo.has("has_detail_page")
						&& jo.getInt("has_detail_page") != 0
						&& jo.has("webpage")) {
					link = jo.getString("webpage");
				}

				ma = new POIMarker(id, HtmlUnescape.unescapeHTML(
						jo.getString("title"), 0), jo.getDouble("lat"),
						jo.getDouble("lng"), jo.getDouble("elevation"), link,
						taskId, colour);
				markers.add(ma);
			}
		}
		return markers;
	}

	private JSONObject convertToJSON(String rawData) {
		try {
			return new JSONObject(rawData);
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
}
